package com.topnav.mc.schedule.job;

import com.js.lib.cache.Cache;
import com.sun.management.OperatingSystemMXBean;
import com.topnav.mc.system.MySystemInfo;
import com.topnav.mc.system.ServerInfo;
import com.topnav.mc.system.SystemInfoUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;

import javax.annotation.Resource;
import java.io.File;
import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 存储空间监控
 * @Author: kenny
 * @Date: 2023/10/9 17:45
 */
@Component
@Slf4j
public class StorageInfoMonitorJob implements Job, Serializable {
    @Resource(name="localCache")
    private Cache cache;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            MySystemInfo mySystemInfo = (MySystemInfo)cache.get("mySystemInfo");
            if (mySystemInfo == null){
                mySystemInfo = new MySystemInfo();
                cache.set("mySystemInfo",mySystemInfo);
            }

            ServerInfo serverInfo = (ServerInfo)cache.get("serverInfo");
            if (serverInfo == null){
                serverInfo = new ServerInfo();
                cache.set("serverInfo",serverInfo);
            }

            // 磁盘使用情况
/*            File[] files = File.listRoots();
            long total=0L;
            long used=0L;
            long free=0L;
            for (File file : files) {
                total += (long) (file.getTotalSpace() * 1.0 / 1024 / 1024 / 1024);
                free += (long) (file.getFreeSpace() * 1.0 / 1024 / 1024 / 1024);
            }

            serverInfo.setTotalDisksize(new DecimalFormat("#.#").format(total)+"G");
            serverInfo.setFreeDisksize(new DecimalFormat("#.#").format(free)+"G");
            used = total - free;*/
            double used = 0.0;
            double free = 0.0;
            List<Map<String, Object>> diskList = SystemInfoUtils.getDiskInfo();
            for(Map<String, Object> map : diskList){
                used = used + Double.parseDouble(map.get("use").toString());
                free = free + Double.parseDouble(map.get("free").toString());
            }
/*            long freeDisksize = Long.valueOf(Math.round(free));
            long usedDisksize = Long.valueOf(Math.round(used));*/

            serverInfo.setUsedDisksize(new DecimalFormat("#.#").format(used)+"G");
            mySystemInfo.setTotalDisksize(Math.round(used) + Math.round(free));
            mySystemInfo.setFreeDisksize(Math.round(free));
            mySystemInfo.setUsedDisksize(Math.round(used));
            //System.out.println(String.format(">>>>>>>>> 存储空间：已用 %s, 剩余 %s",serverInfo.getUsedDisksize(),serverInfo.getFreeDisksize()));
        }catch (Exception e) {
            log.error(e.toString());
        }
    }
}
